﻿/************************************************************************
* Copyright (c) 2018 All Rights Reserved.
* 命名空间：DavidHelper
* 文件名：NlogHelper
* 创建人：David Rodríguez
* 创建时间：2018/7/18 星期三 10:13:50
* 描述：Nlog帮助类
*************************************************************************/

using System;
using System.Diagnostics;
using System.Reflection;
using System.ServiceModel.Activation.Configuration;
using NLog;
using NLog.Config;

namespace DavidHelper
{
    public class NlogHelper
    {
        private static Logger _logger = LogManager.GetCurrentClassLogger();

        static NlogHelper()
        {
            LogManager.Configuration =
                new XmlLoggingConfiguration(AppDomain.CurrentDomain.BaseDirectory +
                                            "\\Nlog.config");
        }


        public enum LogStatus
        {
            Debug,
            Info,
            Error,
            Fatal,
            Warn
        }

        /// <summary>
        /// 日志写入通用方法(建议使用)
        /// </summary>
        /// <param name="msg">日志内容</param>
        /// <param name="logType"> 日志类别
        ///     类别: 1.Debug
        ///           2.Info
        ///           3.Error
        ///           4.Fatal
        ///           5.Warn
        /// </param>
        /// <param name="loginState">登录状态  true:有用户登录信息 false 无用户登录信息</param>
        /// <remarks>
        ///     注：默认类型为Info 可以配置其他日志 logType用于反射 规则一定要准确
        ///     例:  1.默认日志 LogWriter("test log");   正常的业务日志
        ///          2.异常日志 LogWriter("test log","Fatal");  try catch 里请使用这个日志级别
        ///     
        /// </remarks>
        public static void LogWriter(string msg, LogStatus logType = LogStatus.Info)
        {
            try
            {
                var logMethod = ""; //调用者类名和方法名
                var trace = new StackTrace();
                //获取是哪个类来调用的  
                var invokerType = trace.GetFrame(1).GetMethod().DeclaringType.Name;
                //获取是类中的那个方法调用的  
                var invokerMethod = trace.GetFrame(1).GetMethod().Name;
                logMethod = invokerType + "." + invokerMethod + " | ";
                //反射执行日志方法
                Type type = typeof(Logger);
                var method = type.GetMethod(logType.ToString(), new Type[] {typeof(String)});
                method.Invoke(_logger, new object[] {logMethod + msg});
            }
            catch
            {
                //日志代码错误,直接记录日志
                Fatal(msg);
                Warn(msg);
            }
        }

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="msg">日志内容</param>
        private static void Debug(string msg)
        {
            _logger.Debug(msg);
        }

        /// <summary>
        /// 信息日志
        /// </summary>
        /// <param name="msg">日志内容</param>
        private static void Info(string msg)
        {
            _logger.Info(msg);
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="msg">日志内容</param>
        private static void Error(string msg)
        {
            _logger.Error(msg);
        }

        /// <summary>
        /// 严重致命错误
        /// </summary>
        /// <param name="msg">日志内容</param>
        private static void Fatal(string msg)
        {
            _logger.Fatal(msg);
        }

        /// <summary>
        /// 警告日志
        /// </summary>
        /// <param name="msg">日志内容</param>
        private static void Warn(string msg)
        {
            try
            {
                _logger.Warn(msg);
            }
            catch
            {
            }
        }
    }
}